var SUCCESS = 200;

var showTopBox = function(content,callback){
    var html = "<div style='display:none;position:fixed;width:250px;background-color:#fff;line-height:40px;height:auto;text-align:center;border :3px #c83b1f solid;' class='TopBox'>"+content+"</div>";
    $("body").append(html);
    var W = $(window).width();
    $(".TopBox").css({
        left:(W - 150) / 2,
        top:0
    });
    $(".TopBox").fadeIn("fast").delay(1000).fadeOut("fast",function(){
        $(".TopBox").remove();
        if(callback != undefined){
            callback();
        }
    });

}
var removeLoginBox = function(){
    $(".loginBox").fadeOut("fast",function(){
        $(".loginBox").remove();
        $(".shadowDiv").remove();
        $("body").css("overflow","auto");
    });
}
var showLoginBox = function(){
    var html = "<style>.loginInput{  width: 400px; height: 50px; margin: 30px auto;}</style>" +
        "<div style='z-index:99998;position: fixed;top:0px;left:0px;width: 100%;height:100%;background-color:#aaa;opacity: .5;' class='shadowDiv'></div>" +
        "<div style='z-index:99999;display:none;position:fixed;width:550px;background-color:#eee;line-height:40px;height:360px;border :1px #aaa solid;padding:10px 75px' class='loginBox'>" +
        "<div style='text-align:center;height:auto;font-size:24px;font-weight: lighter;'>登陆Fiidee Blog</div>" +
        "<form action='"+CONTEXT+"/user/login' method='post' id='loginForm'>" +
        "<input type='text' name='loginCode' class='form-control loginInput' placeholder='用户名/邮箱/手机号码'  />" +
        "<input type='password' name='password' class='form-control loginInput' placeholder='密码'  />" +
        "<input type='submit' style='width: 120px;height: 40px; line-height: 40px;background-color: #dd5136;color:#fff;border:0;margin:10px auto;display: block;' value='登陆'/>" +
        "</form>" +
        "<script>" +
        "$('#loginForm').on('submit',function(event){event.preventDefault();" +
        "console.log('here');" +
        "var loginCode = $('input[name=\"loginCode\"]').val();" +
        "var password = $('input[name=\"password\"]').val();" +
        "removeLoginBox();" +
        "doLogin(loginCode,password,function(){window.location.reload();})" +
        "})" +
        "</script>" +
        "<a href='javascript:removeLoginBox();'>取消登陆</a>" +
        "</div>";
    $("body").append(html);
    $("body").css("overflow","hidden");
    var W = $(window).width();
    $(".loginBox").css({
        left:(W - 550) / 2,
        top:100
    });
    $(".loginBox").fadeIn("fast");

}

var doLogin = function(loginCode,password, callback){
    $.get(CONTEXT+'/user/login?loginCode='+loginCode+"&password="+password, function(data) {
        if(data.ret == SUCCESS){

            showTopBox("登陆成功",callback);
        }else{
            showTopBox("登陆失败："+data.msg);
        }
    });
}

var doLogout = function(callback){
	$.get(CONTEXT+'/user/logout', function(data) {
        if(data.ret == SUCCESS){
            showTopBox("退出登陆成功",callback);
        }else{
            showTopBox("退出登陆失败："+data.msg);
        }
	});
}

var doColBlog = function (blogid,callback){
    $.get(CONTEXT + "/blog/collection?blogid="+blogid , function(data){
        if(data.ret == SUCCESS){
            showTopBox("收藏文章成功",callback);
        }else{
            showTopBox("收藏文章失败"+data.msg);
        }
    })
}

var doCancelColBlog = function (blogid,callback){
    $.get(CONTEXT + "/blog/cancelcollection?blogid="+blogid , function(data){
        if(data.ret == SUCCESS){
            showTopBox("取消收藏文章成功",callback);
        }else{
            showTopBox("取消收藏文章失败"+data.msg);
        }
    })
}

var doTrash = function (blogid,callback){
    $.get(CONTEXT + "/blog/trash?blogid="+blogid , function(data){
        if(data.ret == SUCCESS){
            showTopBox("回收文章成功",callback);
        }else{
            showTopBox("回收文章失败"+data.msg);
        }
    })
}

var doRecovery = function (blogid,callback){
    $.get(CONTEXT + "/blog/recovery?blogid="+blogid , function(data){
        if(data.ret == SUCCESS){
            showTopBox("恢复文章成功",callback);
        }else{
            showTopBox("恢复文章失败"+data.msg);
        }
    })
}

var doTop = function (blogid,callback){
    $.get(CONTEXT + "/blog/top?blogid="+blogid , function(data){
        if(data.ret == SUCCESS){
            showTopBox("置顶文章成功",callback);
        }else{
            showTopBox("置顶文章失败"+data.msg);
        }
    })
}

var doCancelTop = function (blogid,callback){
    $.get(CONTEXT + "/blog/canceltop?blogid="+blogid , function(data){
        if(data.ret == SUCCESS){
            showTopBox("取消置顶文章成功",callback);
        }else{
            showTopBox("取消置顶文章失败"+data.msg);
        }
    })
}


$(document).ready(function(){
    $(".logoutAction").on('click', function(event) {
        doLogout(function(){console.log("退出登陆！");window.location.reload();});
    });
    $(".showLoginBox").on('click',function(event){
        showLoginBox();
    });

    $('.imgUrl,.imgAction').mouseover(function()
    {
        $(this).animate({
            opacity:"1"
        })
    }).mouseout(function()
    {
        $(this).animate({
            opacity:".6"
        })
    }).click(function(){
        if($(this).attr("class") == 'imgUrl'){
            var url = $(this).attr("href");
            window.location.href = CONTEXT + url;
        }
    });
});
